#include "record.h"

#define MAX_MINUTE 5
#define MAX_SECOND 59
LV_FONT_DECLARE(arial_18);

static lv_obj_t * record_bar;
static lv_obj_t * record_list;
static lv_obj_t * record_pause_label;
static lv_obj_t * time_label;

static void event_handler(lv_event_t * e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    int index = (int)(intptr_t)lv_obj_get_user_data(btn);
    record_app_data_t* record_app_data = record_get_data();

    printf("Button %d clicked\n", index);
    if (code == LV_EVENT_CLICKED) {
        record_play_index(record_app_data, index);
    }
}

void create_record_list(void)
{
    /*Create a list*/
    lv_obj_t * list = lv_list_create(record_list);
    lv_obj_set_size(list, lv_pct(100), lv_pct(100));
    lv_obj_set_align(list, LV_ALIGN_TOP_MID);
    lv_obj_set_style_border_side(list, LV_BORDER_SIDE_TOP,0); 
    
    static lv_style_t list_label_style;
    lv_style_init(&list_label_style);
    lv_style_set_text_align(&list_label_style, LV_TEXT_ALIGN_LEFT);
    lv_style_set_text_font(&list_label_style, &arial_18);

    record_app_data_t* record_app_data = record_get_data();

    /*Add buttons to the list*/
    lv_obj_t * btn;
    for(int i = 0; i < record_app_data->sum_track_no; i++) {
        btn = lv_list_add_btn(list, LV_SYMBOL_AUDIO, record_app_data->file_iterator->list[i]);
        lv_obj_set_width(btn, lv_pct(100));
        lv_obj_set_height(btn, lv_pct(25));
        lv_obj_set_flex_flow(btn, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(btn, 
                            LV_FLEX_ALIGN_SPACE_BETWEEN,
                            LV_FLEX_ALIGN_CENTER,
                            LV_FLEX_ALIGN_CENTER);
        lv_obj_set_user_data(btn, (void*)(intptr_t)i);

        // lv_obj_t* icon = lv_obj_get_child(btn, 0);

        lv_obj_t * label = lv_obj_get_child(btn, 1); 
        lv_obj_add_style(label, &list_label_style, 0);
        lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
    }
}

void record_ui_back_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    
    if (code == LV_EVENT_CLICKED)
    {
        printf("back button clicked\n");
        sys_app_quit(sys_get_cur_app());
    }
}

void record_ui_list_btn_event_cb(lv_event_t *event)
{
    static uint8_t list_flag = 0;
    lv_event_code_t code = lv_event_get_code(event);
    record_app_data_t* record_app_data = record_get_data();

    if (code == LV_EVENT_CLICKED)
    {
        printf("list button clicked\n");
        if(list_flag == 0)
        {
            record_iterator_init(record_app_data);
            create_record_list();
            lv_obj_add_flag(record_bar, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(record_list, LV_OBJ_FLAG_HIDDEN);
        }else {
            record_iterator_deinit(record_app_data);
            lv_obj_clean(record_list);
            lv_obj_add_flag(record_list, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(record_bar, LV_OBJ_FLAG_HIDDEN);
            audio_player_pause();
        }
        list_flag = ~list_flag;
    }
}

void record_ui_pause_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    const char * text = lv_label_get_text(record_pause_label);
    record_state_t state = record_get_current_state();
    record_app_data_t* record_app_data = record_get_data();
    if (code == LV_EVENT_CLICKED)
    {
        printf("text: %s\n", text);
        if (strcmp(text, LV_SYMBOL_PAUSE) == 0) {
            printf("pause button clicked\n");
            if (state & RECORD_STATE_RECORDING) {
                recorder_pause(record_app_data);
            }
            lv_label_set_text(record_pause_label, LV_SYMBOL_PLAY);
        } else if (strcmp(text, LV_SYMBOL_PLAY) == 0) {
            printf("play button clicked\n");
            if (state & RECORD_STATE_IDLE) {
                recorder_start(record_app_data);
            } else if (state & RECORD_STATE_PAUSED) {
                recorder_resume(record_app_data);
            }
            lv_label_set_text(record_pause_label, LV_SYMBOL_PAUSE);
        }
    }
}

void record_ui_save_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    record_state_t state = record_get_current_state();
    record_app_data_t* record_app_data = record_get_data();
    if (code == LV_EVENT_CLICKED)
    {
        printf("save button clicked\n");
        if ((state & RECORD_STATE_RECORDING) || (state & RECORD_STATE_PAUSED)) {
            recorder_stop(record_app_data);
            lv_label_set_text(record_pause_label, LV_SYMBOL_PLAY);
        }
    }
}

void updata_record_ui_time(uint32_t sec)
{
    char time_str[6]; 

    uint32_t minutes = sec / 60;
    uint32_t seconds = sec % 60;

    minutes = (minutes > MAX_MINUTE) ? MAX_MINUTE : minutes;
    seconds = (seconds > MAX_SECOND) ? MAX_SECOND : seconds;

    snprintf(time_str, sizeof(time_str),"%02u:%02u", (unsigned)minutes, (unsigned)seconds);   
    lv_label_set_text(time_label, time_str);
}

lv_obj_t* record_lvgl_register(void)
{
    lv_obj_t* lv_obj = lv_obj_create(NULL);
    if (lv_obj == NULL) {
        LV_LOG_ERROR("Failed to create main music app object.");
        return NULL;
    }

    static lv_style_t style_btn;
    lv_style_init(&style_btn);
    lv_style_set_bg_opa(&style_btn, 0);
    lv_style_set_pad_all(&style_btn, 0);
    lv_style_set_shadow_opa(&style_btn, 0);

    //TODO: add lvgl code here
    lv_obj_t * nav_bar = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(nav_bar, 0, LV_PART_MAIN);
    lv_obj_set_size(nav_bar, LV_HOR_RES, LV_VER_RES / 5);
    lv_obj_align(nav_bar, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_radius(nav_bar, 0, 0);
    lv_obj_set_style_bg_color(nav_bar, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(nav_bar, 0, LV_PART_MAIN);
    lv_obj_clear_flag(nav_bar, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * back_btn = lv_btn_create(nav_bar);
    lv_obj_set_size(back_btn, 100, 40);
    lv_obj_add_style(back_btn, &style_btn, 0);
    lv_obj_align(back_btn, LV_ALIGN_LEFT_MID, 0, 0);  
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_align(back_label, LV_ALIGN_LEFT_MID, 10, 0);
    lv_label_set_text(back_label, LV_SYMBOL_LEFT " back");
    lv_obj_set_style_text_color(back_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(back_label, &lv_font_montserrat_24, 0);
    lv_obj_add_event_cb(back_btn, record_ui_back_btn_event_cb, LV_EVENT_CLICKED, lv_obj);

    lv_obj_t * list_btn = lv_btn_create(nav_bar);
    lv_obj_set_size(list_btn, 50, 40);
    lv_obj_add_style(list_btn, &style_btn, 0);
    lv_obj_align(list_btn, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_t * list_label = lv_label_create(list_btn);
    lv_obj_align(list_label, LV_ALIGN_RIGHT_MID, -10, 0);
    lv_label_set_text(list_label, LV_SYMBOL_LIST);
    lv_obj_set_style_text_color(list_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(list_label, &lv_font_montserrat_24, 0);
    lv_obj_add_event_cb(list_btn, record_ui_list_btn_event_cb, LV_EVENT_CLICKED, NULL);

    record_bar = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(record_bar, 0, LV_PART_MAIN);
    lv_obj_set_size(record_bar, LV_HOR_RES, LV_VER_RES * 4 / 5);
    lv_obj_align(record_bar, LV_ALIGN_TOP_LEFT, 0, LV_VER_RES / 5);
    lv_obj_set_style_radius(record_bar, 0, 0);
    lv_obj_set_style_bg_color(record_bar, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(record_bar, 0, LV_PART_MAIN);   
    lv_obj_clear_flag(record_bar, LV_OBJ_FLAG_SCROLLABLE);

    time_label = lv_label_create(record_bar);
    lv_label_set_text(time_label, "00:00");
    lv_obj_set_style_text_font(time_label, &lv_font_montserrat_48, 0);
    lv_obj_align(time_label, LV_ALIGN_CENTER, 0, -40);

    lv_obj_t * pause_btn = lv_btn_create(record_bar);
    lv_obj_set_size(pause_btn, 60, 60);
    lv_obj_add_style(pause_btn, &style_btn, 0);
    lv_obj_align(pause_btn, LV_ALIGN_BOTTOM_LEFT, 60, -10);
    record_pause_label = lv_label_create(pause_btn);
    lv_obj_align(record_pause_label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(record_pause_label, LV_SYMBOL_PLAY);
    lv_obj_set_style_text_color(record_pause_label, lv_color_hex(0x707070), 0);
    lv_obj_set_style_text_font(record_pause_label, &lv_font_montserrat_36, 0);
    lv_obj_add_event_cb(pause_btn, record_ui_pause_btn_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * save_btn = lv_btn_create(record_bar);
    lv_obj_set_size(save_btn, 60, 60);
    lv_obj_add_style(save_btn, &style_btn, 0);
    lv_obj_align(save_btn, LV_ALIGN_BOTTOM_RIGHT, -60, -10);
    lv_obj_t * save_label = lv_label_create(save_btn);
    lv_obj_align(save_label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(save_label, LV_SYMBOL_SAVE);
    lv_obj_set_style_text_color(save_label, lv_color_hex(0x707070), 0);
    lv_obj_set_style_text_font(save_label, &lv_font_montserrat_36, 0);
    lv_obj_add_event_cb(save_btn, record_ui_save_btn_event_cb, LV_EVENT_CLICKED, NULL);

    record_list = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(record_list, 0, LV_PART_MAIN);
    lv_obj_set_size(record_list, LV_HOR_RES, LV_VER_RES * 4 / 5);
    lv_obj_align(record_list, LV_ALIGN_TOP_LEFT, 0, LV_VER_RES / 5);
    lv_obj_set_style_radius(record_list, 0, 0);
    lv_obj_set_style_bg_color(record_list, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(record_list, 0, LV_PART_MAIN);   
    lv_obj_clear_flag(record_list, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(record_list,  LV_OBJ_FLAG_HIDDEN);

    return lv_obj;
}

static const ui_key_map_t common_keymap[] = {
    // { KEY_POWER,    KEY_TYPE_SHORT_UP,      RECORD_STATE_IDLE,          RECORD_MSG_CMD_START},
    // { KEY_POWER,    KEY_TYPE_SHORT_UP,      RECORD_STATE_RECORDING,     RECORD_MSG_CMD_PAUSE},
    // { KEY_POWER,    KEY_TYPE_SHORT_UP,      RECORD_STATE_PAUSED,        RECORD_MSG_CMD_RESUME},
    // { KEY_POWER,    KEY_TYPE_LONG_DOWN,     RECORD_STATE_RECORDING,     RECORD_MSG_CMD_STOP},
    { KEY_RESERVED,     0,  0,  0 },
};

static int record_view_proc(uint8_t view_id, uint8_t msg_id, uint32_t ui_event)
{
	ESP_LOGI(TAG, "view_id %d msg_id %d ui_event %lu\n", view_id, msg_id, ui_event);

	switch (msg_id) {
        case MSG_VIEW_CREATE:
            ui_manager_load_acr_anim(RECORD_VIEW, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 0, false);
        break;

        case MSG_VIEW_DELETE:
            lv_obj_clean(record_list);
            lv_obj_add_flag(record_list, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(record_bar, LV_OBJ_FLAG_HIDDEN);
            
            ui_manager_load_acr_anim(MENU_VIEW, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 0, false);
        break;
	
        default:
        break;
	}
	return 0;
}

void record_view_init(void)
{
    ui_view_info_t view_info;

	memset(&view_info, 0, sizeof(ui_view_info_t));

	view_info.view_proc = record_view_proc;
	view_info.view_key_map = common_keymap;
	view_info.view_get_state = record_get_current_state;
	view_info.order = 1;
	view_info.task_name = APP_TASK_NAME;

	ui_view_create(RECORD_VIEW, &view_info);
}

void record_view_exit(void)
{
	ui_view_delete(RECORD_VIEW);
}